package com.yc.booktable.service.impl;

import com.yc.booktable.entity.AdminUser;
import com.yc.booktable.entity.Bookinfo;
import com.yc.booktable.entity.Feedback;
import com.yc.booktable.entity.Ubookinfo;
import com.yc.booktable.mapper.BookinfoMapper;
import com.yc.booktable.mapper.UbookinfoMapper;
import com.yc.booktable.service.IBookinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.booktable.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zbh
 * @since 2022-06-06
 */
@Service
public class BookinfoServiceImpl extends ServiceImpl<BookinfoMapper, Bookinfo> implements IBookinfoService {
    @Resource
    private BookinfoMapper bookinfoMapper;
    @Resource
    private UbookinfoMapper mapper;
    @Override
    public Result add(Date time, String people, String message, HttpSession session) {
        Integer number = 0;
        try{
            number = Integer.parseInt(people);
        }catch (NumberFormatException e){
            return new Result(0,e.getMessage(),number);
        }
        if(session.getAttribute("loginedUser")==null){
            return new Result(0,"您还没有登陆",null);
        }
        if(time.getTime()<System.currentTimeMillis()){
            return new Result(0,"预约时间已过",null);
        }
        Bookinfo bookinfo = new Bookinfo(null, time, number, message, 0);
        System.out.println(bookinfo.getBid());
        int insert = bookinfoMapper.insert(bookinfo);
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        Ubookinfo ubookinfo = new Ubookinfo(0, user.getId(), bookinfo.getBid());
        int insert1 = mapper.insert(ubookinfo);
        if(insert!= 1&& insert1!=1){
            return new Result(0,"预约失败",null);
        }
        return new Result(1,"订餐成功",insert);
    }
}

